package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.ConDetailsModel;
import com.banshuibao.bsb_android.model.ConsutationModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.TimeUtil;

public class ConDetailsActivity extends BaseActivity implements View.OnClickListener {

    private ImageView headImage_one, headImage_two;
    private TextView name_one, name_two;
    private TextView position_one, position_two;
    private TextView content_one, content_two;
    private TextView date_one, date_two;
    private TextView text;
    private ImageView image_one, image_two, image_three;
    private String[] split;
    private String userId;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_con_details);
        initView();
        setData();
        getData();

    }

    private void setData() {
        GetLoginInfo getLoginInfo = GetLoginInfo.getLoginInfoInstance(this).readUser();
        String role = getLoginInfo.getRole();
        String name = getLoginInfo.getName();
        String headImg = getLoginInfo.getHeadImg();
        String phoneNumber = getLoginInfo.getPhoneNumber();
        String position = getLoginInfo.getPosition();
        String duty = getLoginInfo.getDuty();
        if (!TextUtils.isEmpty(name)) {
            name_two.setText(name);
        } else {
            if (!TextUtils.isEmpty(phoneNumber)) {
                name_two.setText(phoneNumber);
            }
        }
        if (!TextUtils.isEmpty(headImg)) {
            ImageUtil.loadImageHead(this, headImg, headImage_two);
        }
        if (role.equals("2")|| role.equals("3")||role.equals("4")){
            if (!TextUtils.isEmpty(duty)) {
                position_two.setText(duty);
            }
        }else {
            if (!TextUtils.isEmpty(position)) {
                position_two.setText(position);
            }
        }
    }

    private void getDetailsPost(String uuid) {
        ConDetailsModel.body body = new ConDetailsModel.body();
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.TAX_RAPID_DETAILS, body, new HttpResponse(this, ConDetailsModel.class) {
            @Override
            public void onResult(Object obj) {
                ConDetailsModel s = (ConDetailsModel) obj;
//                LogUtil.e(s);
                ConDetailsModel.DataBean data = s.getData();
                String status = data.getStatus();
                if (status.equals("0")) {
                    text.setText("未被采纳");
                } else if (status.equals("1")) {
                    text.setText("已被采纳");
                }
                String content = data.getContent();
                long ansDate = data.getAnsDate();
                content_two.setText(content);
                String time = null;
                if (ansDate != 0) {
                    time = TimeUtil.format(ansDate);
                } else {
                    time = "";
                }
                date_two.setText(time);
            }
        });
    }

    private void getData() {
        ConsutationModel.DataBean item = (ConsutationModel.DataBean) getIntent().getSerializableExtra("item");
        if (item != null) {
            String uuid = item.getUuid();
            getDetailsPost(uuid);

            userId = item.getUserId();
            String headImg = item.getHeadImg();
            String name = item.getName();
            String positiotn = item.getPositiotn();
            String content = item.getContent();
            String imgs = item.getImgs();
            long createDate = item.getCreateDate();

            if (!TextUtils.isEmpty(headImg)) {
                ImageUtil.loadImageHead(this, headImg, headImage_one);
            }
            if (!TextUtils.isEmpty(name)) {
                name_one.setText(name);
            }
            if (!TextUtils.isEmpty(positiotn)) {
                position_one.setText(positiotn);
            }
            if (!TextUtils.isEmpty(content)) {
                content_one.setText(content);
            }
            String time = null;
            if (createDate != 0) {
                time = TimeUtil.format(createDate);
            } else {
                time = "";
            }
            date_one.setText(time);
            if (!TextUtils.isEmpty(imgs)) {
                split = imgs.split(",");
                if (split.length > 0) {
                    if ("null".equals(split[0]))
                        return;
                    if (split.length == 1) {
                        ImageUtil.loadImage(this, split[0],image_one);
                        image_two.setVisibility(View.GONE);
                        image_three.setVisibility(View.GONE);
                    } else if (split.length == 2) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        ImageUtil.loadImage(this, split[1], image_two);
                        image_three.setVisibility(View.GONE);
                    } else if (split.length == 3) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        ImageUtil.loadImage(this, split[1], image_two);
                        ImageUtil.loadImage(this, split[2], image_three);
                    }
                }
            } else {
                image_one.setVisibility(View.GONE);
                image_two.setVisibility(View.GONE);
                image_three.setVisibility(View.GONE);
                linearLayout.setVisibility(View.GONE);
            }
        }
    }

    private void initView() {
        setTitleText("快速咨询详情");
        headImage_one = (ImageView) findViewById(R.id.head_iv);
        headImage_two = (ImageView) findViewById(R.id.head_iv_two);
        name_one = (TextView) findViewById(R.id.name);
        name_two = (TextView) findViewById(R.id.name_two);
        position_one = (TextView) findViewById(R.id.position);
        position_two = (TextView) findViewById(R.id.position_two);
        content_one = (TextView) findViewById(R.id.content);
        content_two = (TextView) findViewById(R.id.content_two);
        date_one = (TextView) findViewById(R.id.date);
        date_two = (TextView) findViewById(R.id.date_two);
        text = (TextView) findViewById(R.id.tv);
        image_one = (ImageView) findViewById(R.id.image_one);
        image_two = (ImageView) findViewById(R.id.image_two);
        image_three = (ImageView) findViewById(R.id.image_three);
        linearLayout = (LinearLayout) findViewById(R.id.liner);
        image_one.setOnClickListener(this);
        image_two.setOnClickListener(this);
        image_three.setOnClickListener(this);
        headImage_two.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_one:
                IntentManager.goBitmapShowActivity(ConDetailsActivity.this, split, 0);
                break;
            case R.id.image_two:
                IntentManager.goBitmapShowActivity(ConDetailsActivity.this, split, 1);
                break;
            case R.id.image_three:
                IntentManager.goBitmapShowActivity(ConDetailsActivity.this, split, 2);
                break;
            case R.id.head_iv_two:
//                setPagePost();
                break;
        }
    }
}
